package com.xjh.him.controller;


import com.xjh.him.annotation.LoginVerify;
import com.xjh.him.common.LoginUser;
import com.xjh.him.domain.UserDO;
import com.xjh.him.dto.user.UserDTO;
import com.xjh.him.dto.user.UserLoginDTO;
import com.xjh.him.dto.user.UserLoginResultDTO;
import com.xjh.him.service.UserServiceImpl;
import com.xjh.him.vo.user.UserLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xjh.tool.Checks;
import org.xjh.tool.common.ErrorMsg;
import org.xjh.tool.dto.ResultDTO;
import org.xjh.tool.vo.AjaxResultVO;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xjh
 * @since 2021-10-09
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;


    @RequestMapping("/login")
    public AjaxResultVO login(@RequestBody UserLoginVO loginVO) {
        if (Checks.isEmpty(loginVO)) {
            return AjaxResultVO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        UserLoginDTO loginDTO = new UserLoginDTO();
        BeanUtils.copyProperties(loginVO, loginDTO);
        ResultDTO<UserLoginResultDTO> resultDTO = userService.login(loginDTO);
        if (resultDTO.isSuccess()) {
            return AjaxResultVO.getSuccessResult(resultDTO.getResult());
        }
        return AjaxResultVO.getFailureResult(resultDTO.getErrMsg());
    }


    @LoginVerify
    @RequestMapping("/me")
    public AjaxResultVO findMe() {
        UserDO loginUser = LoginUser.getLoginUser();
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(loginUser, userDTO);
        return AjaxResultVO.getSuccessResult(userDTO);
    }


    @LoginVerify
    @RequestMapping("/findUser")
    public AjaxResultVO findUser(Long id) {
        ResultDTO<UserDTO> resultDTO = userService.findUser(id);
        if (resultDTO.isSuccess()) {
            return AjaxResultVO.getSuccessResult(resultDTO.getResult());
        }
        return AjaxResultVO.getFailureResult(resultDTO.getErrMsg());
    }


    @LoginVerify
    @RequestMapping("/updateInfo")
    public AjaxResultVO updateInfo(@RequestBody UserDTO userDTO) {
        ResultDTO<Boolean> resultDTO = userService.updateInfo(userDTO);
        if (resultDTO.isSuccess()) {
            return AjaxResultVO.getSuccessResult(true);
        }
        return AjaxResultVO.getFailureResult(resultDTO.getErrMsg());
    }

}
